<template>
  <div class="main">
    <div class="title">
      <div class="titleBlock"></div>
      <div class="titleText">新增套餐信息</div>
    </div>
    <div class="card1">
      <div class="card1Title">新增套餐信息</div>
      <div class="card1Section">
        <div class="form1">
          <el-form
            ref="ruleFormRef"
            :model="ruleForm"
            :rules="rules"
            label-width="120px"
            class="demo-ruleForm"
            :size="formSize"
          >
            <el-form-item label="套餐ID" prop="packageID">
              <el-input v-model="ruleForm.packageID" />
            </el-form-item>
            <el-form-item label="套餐名称" prop="packageName">
              <el-input
                v-model="ruleForm.packageName"
                placeholder="请输入内容"
              />
            </el-form-item>
            <el-form-item label="套餐图片" prop="packageImage">
              <el-upload
                class="avatar-uploader"
                action="/api/upload1"
                name="file"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>
            <el-form-item label="套餐类型" prop="typeName">
              <el-select
                v-model="ruleForm.typeName"
                placeholder="请选择套餐类型"
              >
                <el-option
                  v-for="item in typeName"
                  :label="item.typeName"
                  :value="item.typeId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="套餐项目" prop="projectName">
              <el-select v-model="ruleForm.projectName" placeholder="请选择">
                <el-option
                  v-for="item in projectName"
                  :label="item.projectName"
                  :value="item.physicalExaminationID"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="套餐价格" prop="packagePrice">
              <el-input v-model="ruleForm.packagePrice" />
            </el-form-item>
            <el-form-item label="套餐介绍" prop="packageIntroduce">
              <el-input
                v-model="ruleForm.packageIntroduce"
                type="textarea"
                :rows="5"
              />
            </el-form-item>
            <el-form-item label="注意事项" prop="announcements">
              <el-input
                v-model="ruleForm.announcements"
                type="textarea"
                :rows="5"
              />
            </el-form-item>
            <el-form-item label="套餐状态" prop="packageStatus">
              <el-switch
                v-model="ruleForm.packageStatus"
                active-text="使用中"
                inactive-text="未使用"
                active-value="true"
                inactive-value="false"
              >
              </el-switch>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="add">保存</el-button>
              <el-button @click="cancel">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, reactive, toRefs, computed, watch } from "vue";
import { useRouter } from "vue-router";
import {
  getTypeName,
  getProjectName,
  addMedicalPlan,
} from "../../utils/api/tiJian";
export default {
  props: {},
  setup() {
    const data = reactive({
      ruleForm: {
        packageID: "",
        packageName: "",
        packageImage: "",
        typeName: "",
        projectName: "",
        packagePrice: "",
        packageIntroduce: "",
        announcements: "",
        packageStatus: "true",
      },
      typeName: [],
      projectName: [],
      imageUrl: "",
    });
    const getTypeName2 = () => {
      getTypeName().then((dataType) => {
        // console.log(dataType);
        if (dataType.data.code == 200) {
          data.typeName = dataType.data.data;
        }
      });
    };
    getTypeName2();

    getProjectName().then((projectData) => {
      // console.log(projectData);
      if (projectData.data.code == 200) {
        data.projectName = projectData.data.data;
      }
    });

    const router = useRouter();

    const cancel = () => {
      router.push({
        path: "/home/hospitalInfoManage/medicalPlan",
      });
    };

    const handleAvatarSuccess = (response, uploadFile) => {
      // console.log(response, uploadFile);
      data.imageUrl = "/api/uploads/" + response.path;
      data.ruleForm.packageImage = response.path;
    };

    const beforeAvatarUpload = (rawFile) => {
      if (rawFile.type !== "image/jpeg") {
        ElMessage.error("Avatar picture must be JPG format!");
        return false;
      } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error("Avatar picture size can not exceed 2MB!");
        return false;
      }
      return true;
    };

    const add = () => {
      addMedicalPlan(data.ruleForm).then((dataL) => {
        console.log(dataL);
        if (dataL.data.code == 200) {
          ElMessage({
            message: "添加成功",
            type: "success",
          });
          router.push({
            path: "/home/hospitalInfoManage/medicalPlan",
          });
        }
      });
    };
    return {
      ...toRefs(data),
      add,
      cancel,
      handleAvatarSuccess,
      beforeAvatarUpload,
    };
  },
};
</script>
<style lang="scss" scoped>
.title {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 0 30px;
  box-sizing: border-box;
  .titleBlock {
    width: 50px;
    height: 13px;
    background-color: #006eff;
    border-radius: 20px;
  }
  .titleText {
    font-size: 30px;
    margin-left: 20px;
  }
}
.card1 {
  box-shadow: 1px 1px 5px 1px rgba(194, 188, 188, 0.486);
  width: 100%;
  margin-bottom: 30px;
  .card1Title {
    padding: 20px;
    box-sizing: border-box;
    color: #006eff;
    background-color: #f2f2f2;
  }
  .card1Section {
    width: 100%;
    background-color: white;
    padding: 40px 0;
    box-sizing: border-box;
    .form1 {
      width: 40%;
    }
  }
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>